%% FIRST LOAD THE DATA
%projectdirDB = 'C:/Users/at1106/Dropbox';
%projectdirDB = '/Users/tamoni/Dropbox';
data = xlsread('./VARdataBBupdated.xlsx','MainData');
% Data		Description
% 1		Date
% 2		CBOE Market Volatility Index, VXO (Avg) 
% 3		Real Gross Domestic Product            (SAAR, Bil.Chn.2009$)  
% 4		Real Personal Consumption Expenditures (SAAR, Bil.Chn.2009.$)  
% 5		Real Personal Consumption Expenditures: Durable Goods (SAAR, Bil.Chn.2009.$)  
% 6		Real Personal Consumption Expenditures: Nondurable Goods (SAAR, Bil.Chn.2009.$)  
% 7		Real Personal Consumption Expenditures: Services (SAAR, Bil.Chn.2009.$) 
% 8		Real Private                Fixed Investment (SAAR, Bil.Chn.2012$)  
% 9		Real Private Nonresidential Fixed Investment (SAAR, Bil.Chn.2012$)  
% 10		Gross Domestic Product: Chain Price Index (SA, 2009=100)  
% 11		Federal Funds [Effective] Rate (Avg, % p.a.) 
% 12		5-Year Treasury Note Yield at Constant Maturity (Avg, % p.a.) 
% 13		Aggregate Hours: Nonfarm Payrolls, Total (SAAR, Bil.Hrs)  
% 14		Money Stock: M2 (SA, Bil.$) 
% 15		Civilian Noninstitutional Population: 16 Years and Over (NSA, Thous)  
% 16		CBOE Market Volatility Index, VIX (Avg)
% 17		Stock Price Index: Standard & Poor's 100 (Avg, Close, Jan-2-76=100)
% 18		Standard & Poor's 500 Stock Price Index (Avg, 1941-43=10)
% 19		Wu-Xia Shadow Rate
% 20		Bloom (2009) Spliced Actual/Implied Volatility
% 21		GDP Deflator Inflation Rate (Annualized)
% 22		Ex-Post Real Rate (Annualized)
% 23		Ex-Post S&P 500 Equity Return (Annualized)
% 24		Ex-Post Equity Premium (Annualized)

date   = data(:,1);
   vxo =     data(:,2);
logvxo = log(data(:,2));

% Convert to per-capita and take logs
% Consumption = Nondurables & Services 
% Investment = Durables & Nonresidential Investment

population     =      data(:,15)/1000000;
logoutput      = log( data(:,3)./population);
logconsumption = log((data(:,6)+data(:,7))             );
% loginvestment= log((data(:,5)+data(:,8))./population );
  loginvestment= log((data(:,5)+data(:,9))./population );
loghours       = log( data(:,13)./population/(20*3));% Hours per day (times 20 days in a month, times 3 for a quarter)
logprices      = log( data(:,10));
logm2          = log( data(:,14));
ffr            =      data(:,11);
shadowrate     =      data(:,19);
logsp500       = log( data(:,18));

START_DATE = 1961;
%   startdate = find(date == 1986);      enddate   = find(date == 2017+3/4); % For VXO which starts in 1986:Q1
    startdate = find(date == START_DATE);enddate   = find(date == 2019+3/4);

%   startdate = find(date == 1970);      enddate   = find(date == 2017+2/4); % Since Leverage starts in 1970:Q1 and ends in 2017:Q3
%   startdate = find(date == 1986+1/4);  enddate   = find(date == 2014+3/4); % Since RA BEX   starts in 1986:Q2 and ends in 2014:Q4
    vardata = [       logoutput logconsumption loginvestment loghours logprices          shadowrate];
%   vardata = [       logoutput logconsumption loginvestment loghours logprices          ffr       ];

% opts = detectImportOptions('F-F_Research_Data_Factors.CSV');
% FF_factors  = readtable('F-F_Research_Data_Factors.CSV',opts);
% FF_factors = table2array(FF_factors);
% ss = find(FF_factors(:,1)==196001);
% se = find(FF_factors(:,1)==201712);
% RmktRf = FF_factors(ss:se,2);
%     Rf = FF_factors(ss:se,5);
% horizonAggreg = 3;
% RmktRfQ = chgfreq(RmktRf+Rf,horizonAggreg,horizonAggreg,0);
% 
% clear opts ss se FF_factors

    vardatawithsp500=[logoutput logconsumption loginvestment loghours logprices logsp500     shadowrate];
%   vardatawithsp500=[logoutput logconsumption loginvestment loghours logprices RmktRfQ/100  shadowrate];
% plot(        [logsp500-logsp500(1)  log(cumprod(1+RmktRfQ/100))]   )    
% plot(detrend([logsp500-logsp500(1)  log(cumprod(1+RmktRfQ/100))],1))    
%% DEFINE VARIABLES
vardata          = vardata(         startdate:enddate,:);
vardatawithsp500 = vardatawithsp500(startdate:enddate,:);

vardata          = [vardata         ];
vardatawithsp500 = [vardatawithsp500];

clearvars -except vardatawithsp500 

save dataQuarterly